<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <!-- <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item> -->
      <!-- <el-form-item label="状态 1正常 0冻结" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态 1正常 0冻结"></el-input>
    </el-form-item> -->
      <el-form-item label="分类名称" prop="catagoryName">
        <el-input
          v-model="dataForm.catagoryName"
          placeholder="分类名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="标签" prop="label">
        <!-- <el-input v-model="dataForm.label" placeholder="标签"></el-input> -->
        <el-button
          icon="el-icon-plus"
          size="small"
          type="primary"
          @click="addHeader1()"
          >添加标签</el-button
        >
        <!-- 动态增加项目 -->
        <!-- 不止一个项目，用div包裹起来 -->
        <div v-for="(item, index) in productLables.headers" :key="index">
          <div class="div-inline distance_style_10">
            <el-form-item style="margin-bottom: 20px">
              <el-input
                style="width: 100px"
                type="text"
                name="StartTime"
                v-model="item.title"
              />
              <el-button @click.prevent="removeHeader1(item, index)"
                >删除</el-button
              >
            </el-form-item>
          </div>
        </div>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      productLables: {
        //del==false 不可删除
        headers: [{ title: "标签名", headerVal: "参数值", del: false }],
      },
      visible: false,
      dataForm: {
        id: 0,
        createTime: "",
        updateTime: "",
        status: "",
        catagoryName: "",
        parentId: "",
        label: "",
      },
      dataRule: {
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" },
        ],
        status: [
          {
            required: true,
            message: "状态 1正常 0冻结不能为空",
            trigger: "blur",
          },
        ],
        catagoryName: [
          { required: true, message: "分类名称不能为空", trigger: "blur" },
        ],
        parentId: [
          { required: true, message: "父类Id不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    addHeader1() {
      this.productLables.headers.push({
        title: "标签名",
      });
    },
    removeHeader1(item, index) {
      this.productLables.headers.splice(index, 1);
    },
    init(id, parentId, saveId) {
      //清空表单
      this.productLables = { headers: [{ title: "标签名" }] };
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (id != null) {
          this.dataForm.parentId = id;
        } else {
          this.dataForm.parentId = saveId;
        }
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/testing/catagory/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.createTime = data.catagory.createTime;
              this.dataForm.updateTime = data.catagory.updateTime;
              this.dataForm.status = data.catagory.status;
              this.dataForm.catagoryName = data.catagory.catagoryName;
              this.dataForm.label = data.catagory.label;
              this.productLables = JSON.parse(data.catagory.label);
              console.log(parentId);
              if (parentId == null || parentId == 0) {
                this.dataForm.parentId = 0;
              } else {
                if (id != null) {
                  //新增
                  this.dataForm.parentId = parentId;
                } else {
                  this.dataForm.parentId = id;
                }
              }
              console.log(parentId);
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.dataForm.label = JSON.stringify(this.productLables); //自定义表单
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/testing/catagory/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime,
              status: this.dataForm.status,
              catagoryName: this.dataForm.catagoryName,
              parentId: this.dataForm.parentId,
              label: this.dataForm.label,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
